package com.vcredit.creditcat.manager;

import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.ILoginPreviewManager;
import com.vcredit.creditcat.enumtype.ProductPreviewEnum;
import com.vcredit.creditcat.model.LoginPreview;
import com.vcredit.creditcat.service.LoginPreviewService;
import com.vcredit.creditcat.service.loginPreview.IProductPreview;
import com.vcredit.creditcat.service.loginPreview.ProductPreviewFactory;
import com.vcredit.creditcat.util.EnumExtUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 登录预检处理类
 * Created by liaoshengjun on 2017/10/27.
 */
@Service(protocol = { "protocol1" }, version = "1.0")
public class LoginPreviewManager implements ILoginPreviewManager{
	@Autowired
	private LoginPreviewService loginPreviewService;
	@Autowired
	private ProductPreviewFactory productPreviewFactory;

	private static Logger logger = LoggerFactory.getLogger(LoginPreviewManager.class);

	@Override
	public void execute(String message) throws Exception{
		if(StringUtils.isBlank(message)){
			return;
		}
		//查询未预检的产品
		List<Long> productIds=loginPreviewService.findUnPreviewProductByMobile(message);
		if(productIds==null || productIds.size()<=0){
			return;
		}
		for(Long productId:productIds){
			//找到产品对应的预检类
			ProductPreviewEnum productPreviewEnum =
					EnumExtUtil.getEnumOnValue(ProductPreviewEnum.class,
							String.valueOf(productId), "code");
			if(productPreviewEnum==null){
				continue;
			}
			String dealClassName=productPreviewEnum.getDealClass();
			if(productPreviewFactory.exitClass(dealClassName)){
				IProductPreview productPreview=productPreviewFactory.getProductPreviewInterface(dealClassName);
				try {
					Integer checkResult=productPreview.checkExist(message);
					LoginPreview loginPreview=new LoginPreview();
					loginPreview.setMobile(message);
					loginPreview.setProductId(Long.valueOf(productPreviewEnum.getCode()));
					loginPreview.setPreviewResult(checkResult);
					loginPreviewService.insert(loginPreview);
				}catch (Exception e){
					logger.error("登录产品预检出错！",e);
					continue;
				}
			}
		}
	}
}
